package com.sj.uabrowser.mvp.video.view.adp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;
import com.sj.uabrowser.R;
import com.sj.uabrowser.app.Util;
import com.sj.uabrowser.mvp.video.bean.NVideoItem;
import com.sj.uabrowser.mvp.video.videoutils.RecyclerItemViewHolder;
import com.sj.uabrowser.mvp.video.view.fragment.VideoFragment;
import com.vondear.rxtool.RxDeviceTool;

import java.util.List;


/**
 * Created by GUO on 2015/12/3.
 */

/**
 * Created by Nelson on 15/11/9.
 */
public class RecyclerBaseAdapter extends RecyclerView.Adapter implements VideoFragment.VideoItemClickListener {

    public final static String TAG = "RecyclerBaseAdapter";

    private List<MultiItemEntity> itemDataList = null;

    private Context context = null;

    private GSYVideoHelper smallVideoHelper;

    private GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;

    public RecyclerBaseAdapter(Context context, List<MultiItemEntity> itemDataList) {
        this.itemDataList = itemDataList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {
        View v = null;
        RecyclerView.ViewHolder holder = null;
        if (viewType == 0) {
            v = LayoutInflater.from(context).inflate(R.layout.common_list_item_video, parent, false);
            View video = v.findViewById(R.id.list_item_container);
//            ViewGroup.LayoutParams lp = video.getLayoutParams();
//            lp.height = (int) ((RxDeviceTool.getScreenWidth(context) - Util.transPX(20)) * 9.0f / 16);
//            video.setLayoutParams(lp);
            holder = new RecyclerItemViewHolder(context, v);
        }

        return holder;

    }

    @Override
    public void onVideoItemClick(String url, int position) {
        if (videoItemClickListener != null) {
            videoItemClickListener.onVideoItemClick(url, position);
        }
    }

    @Override
    public void onVideoItemPause() {
        if(videoItemClickListener!=null)videoItemClickListener.onVideoItemPause();
    }

    public VideoItemClickListener videoItemClickListener;

    public void setVideoItemClickListener(VideoItemClickListener videoItemClickListener) {
        this.videoItemClickListener = videoItemClickListener;
    }

    public interface VideoItemClickListener {
        void onVideoItemClick(String url, int position);
        void onVideoItemPause();
    }


    public interface AdpShareItemClick {
        void OnSharedClick(int position);
    }

    public interface AdpReplayItemClick {
        void onReplayCick(int position);
    }



    private AdpShareItemClick adpShareItemClick;
    private AdpReplayItemClick adpReplayItemClick;


    public void setAdpReplayItemClick(AdpReplayItemClick adpReplayItemClick) {
        this.adpReplayItemClick = adpReplayItemClick;
    }

    public void setAdpShareItemClick(AdpShareItemClick adpShareItemClick) {
        this.adpShareItemClick = adpShareItemClick;
    }

    private RecyclerItemViewHolder holder;

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        MultiItemEntity bean = itemDataList.get(position);
        if (getItemViewType(position) == 0) {
            RecyclerItemViewHolder recyclerItemViewHolder = (RecyclerItemViewHolder) holder;
            setVideoItemClickListener(recyclerItemViewHolder);
            recyclerItemViewHolder.setShareListener(new RecyclerItemViewHolder.ShareListener() {
                @Override
                public void onShareItemClick(int position) {
                    if (adpShareItemClick != null) adpShareItemClick.OnSharedClick(position);
                }
            });
            recyclerItemViewHolder.setUpdateUrlListener(new RecyclerItemViewHolder.UpdateUrlListener() {
                @Override
                public void onUpdateUrl(int position) {
                    if (adpReplayItemClick != null) adpReplayItemClick.onReplayCick(position);
                }
            });
            recyclerItemViewHolder.setVideoHelper(smallVideoHelper, gsySmallVideoHelperBuilder);
            recyclerItemViewHolder.setRecyclerBaseAdapter(this);
            recyclerItemViewHolder.onBind(position, (NVideoItem) bean);
        }
    }

    @Override
    public int getItemCount() {
        return itemDataList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return itemDataList.get(position).getItemType();
    }

    public void setListData(List<MultiItemEntity> data) {
        itemDataList = data;
        notifyDataSetChanged();
    }

    public GSYVideoHelper getVideoHelper() {
        return smallVideoHelper;
    }


    public void setVideoHelper(GSYVideoHelper smallVideoHelper, GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder) {
        this.smallVideoHelper = smallVideoHelper;
        this.gsySmallVideoHelperBuilder = gsySmallVideoHelperBuilder;
    }



}
